//ST*****************************************表格编辑组件*********************************************
function JyyTable() {
}
JyyTable.init = function(id, config, style, callback) {
	var ctrl = document.getElementById(id);
    if (!ctrl) {
        return;
    }
	JyyTable.ctrl = ctrl;
	JyyTable.config = {
		data:[],
		listData:{},
		hideCols:[],
		isShowCheckBox:true,
		isShowFun:false,
		editColsIndex:[],
		page:{
			isShowPage:false,
			pageArea:[5,10,20,30,50],
			option:{
				currentPage:1,
				numberOfPages:5,
				totalPages:1,
				totalSize:0,
				pageSize:20,
				itemTexts: function (type, page, current) {
					switch (type) {
						case "first": return "首页";
						case "prev": return "上一页";
						case "next": return "下一页";
						case "last": return "末页";
						case "page": return page;
					}
				}
			}
		}
	};
	ctrl.setStyle = JyyCommon.setStyle;
    ctrl.dispose = JyyCommon.dispose;
    ctrl.dispose();
    
	if (id) {
    	ctrl.id = id;
	}
	if (style) {
    	ctrl.setStyle(ctrl, style);
    }
    ctrl.jyytype = "JyyTable";
    ctrl.config = config;
    ctrl.callback = callback;
    ctrl.isHideCols = true;
	
	ctrl.funButton = '<td name="buttons" style="min-width: 65px;">'+
					 '	<div class="btn-group pull-right" style="float:none !important">'+
					 '		<button id="jyytable_edit" type="button" class="btn btn-sm btn-default" style="padding: 3px 6px;" onclick=JyyTable.funClick(this,"edit");>'+
					 '			<span class="glyphicon glyphicon-pencil" ></span>'+
					 '		</button>'+
					 '		<button id="jyytable_elim" type="button" class="btn btn-sm btn-default" style="padding: 3px 6px;" onclick=JyyTable.funClick(this,"elim");>'+
					 '			<span class="glyphicon glyphicon-trash" ></span>'+
					 '		</button>'+
					 '		<button id="jyytable_acep" type="button" class="btn btn-sm btn-default" style="display:none;padding: 3px 6px;" onclick=JyyTable.funClick(this,"acep");>'+ 
					 '			<span class="glyphicon glyphicon-ok" ></span>'+
					 '		</button>'+
					 '		<button id="jyytable_canc" type="button" class="btn btn-sm btn-default" style="display:none;padding: 3px 6px;" onclick=JyyTable.funClick(this,"cancel");>'+ 
					 '			<span class="glyphicon glyphicon-remove" ></span>'+
					 '		</button>'+
				     '	</td>'+
					 '</div>';
	
	ctrl.checkConfig = function(config, newConfig) {
		for(key in config) {
			if(JyyCommon.isObject(newConfig[key])){
				this.checkConfig(config[key], newConfig[key]);
			}else{
				if(JyyCommon.isEmpty(newConfig[key])){
					newConfig[key] = config[key];
				}
			}
		}
	}
	ctrl.checkConfig(JyyTable.config, config);
	
    ctrl.init = function() {
    	var data = this.config.data;
    	if(data.length == 0) {
    		return;
    	}
    	//表头行
    	var thead = "";
	    for(var i = 0; i < data[0].length; i++){
	    	var value = data[0][i];
	    	if(JyyCommon.isObject(value)){
	    		thead += "<td>" + value.name + "</td>";
	    	}else{
	    		thead += "<td>" + value + "</td>";
	    	}
	    }
	    //数据行
    	var tbody = "";
	    for(var i = 1; i < data.length; i++){
	    	var row = data[i];
	    	tbody += "<tr posIndex="+ i +">";
	    	for(var j = 0; j < row.length; j++){
	    		var value = row[j];
	    		if(JyyCommon.isObject(value)){
	    			tbody += "<td code=" + value.code + ">" + value.name + "</td>";
		    	}else{
		    		tbody += "<td>" + value + "</td>";
		    	}
	    	}
	    	tbody += "</tr>";
	    }
	    //基础table
		var tableStr =  '<div style="position: relative;overflow: hidden;border-width: 0;width: 100%;height: 100%;">'+
				       
						'	<div class="table-responsive" style="overflow: hidden;position: absolute;z-index: 1001;border-width: 0;padding: 0;margin: 0;width: auto;">'+
						'		<table id="jyytable_title" class="table table-bordered table-striped" style="margin-bottom: 0px;display:none;">'+
						'			<thead>'+
						'				<tr style="white-space: nowrap;font-weight: bold;background-color: rgb(64, 144, 231);color: #fff;">' + thead + '</tr>'+
						'			</thead>'+
						'		</table>'+
						'	</div>'+
						'	<div class="table-responsive" style="overflow: auto;height: 100%;padding: 0;margin: 0;">'+
						'		<table class="table table-bordered table-striped" id="jyytable">'+
						'			<thead>'+
						'				<tr style="white-space: nowrap;font-weight: bold;background-color: rgb(64, 144, 231);color: #fff;">' + thead + '</tr>'+
						'			</thead>'+
						'			<tbody>' + tbody + '</tbody>'+
						'		</table>'+
						'	</div>'+
						'</div>';
		
		if(this.config.page.isShowPage){
		    //分页每页显示条目数
		    var pageSelect = '<select class="form-control input-sm" style="color: #2c9d80;">';
		    var pageArea = this.config.page.pageArea;
		    for(var i = 0; i < pageArea.length; i++){
		    	var row = pageArea[i];
		    	pageSelect += '<option value="' + row + '">' + row + '条/页</option>';
		    }
		    pageSelect += '</select>';
		    
			tableStr = '<div id="jyytable_total" style="position: absolute;left: 0px;bottom: 0px;width: 270px;height:50px;line-height: 50px;"><span style="position: absolute;width: 80px;text-align: right;color: #2c9d80;">共'+this.config.page.option.totalSize+'条数据</span></div>'+
					   '<div id="jyytable_table" style="position: absolute;top: 0px;left: 0px;right: 0px;bottom: 50px;">' + tableStr + '</div>'+
					   '<div style="position: absolute;left: 0px;bottom: 0px;right: 360px;height:50px;"><div id="jyytable_page" style="float: right;margin: 10px 10px;"></div></div>'+
					   '<div id="jyytable_size" style="position: absolute;right: 270px;bottom: 0px;width: 97px;padding-top: 10px;height: 50px">' + pageSelect + '</div>'+
					   '<div id="jyytable_skip" style="position: absolute;right: 0px;bottom: 0px;width: 270px;height:50px;line-height: 50px;"><span style="position: absolute;width: 130px;text-align: right;color: #2c9d80;">共有'+this.config.page.option.totalPages+'页,跳至</span><div style="position: absolute;width: 50px;top: 10px;left: 135px;"><input class="form-control input-sm" style="padding: 0px 2px;"></div><span style="position: absolute;width: 10px;left: 190px;color: #2c9d80;">页</span><button style="position: absolute;left: 210px;top: 14px;" type="button" class="btn btn-primary btn-xs">GO</button></div>'
					   
		}
		
		this.innerHTML = tableStr;
		
		//如果显示分页，则为分页绑定事件。
		if(this.config.page.isShowPage){
			var pageSize = this.config.page.option.pageSize;
			this.config.page.option.bootstrapMajorVersion = 1;
			this.config.page.option.onPageClicked = this.onPageEvent;
			$(this).find('#jyytable_page').bootstrapPaginator(this.config.page.option);
			$(this).find('#jyytable_size').find('option').each(function(){
				if ($(this).val() == pageSize) {
			         $(this).attr("selected", "selected");
			    }
			})
		}
		//每行前面加复选框
		if(this.config.isShowCheckBox){
			this.showCheckBox();
		}
		//每行末尾加操作按钮
		if(this.config.isShowFun){
			this.showFun();
		}
		//隐藏默认列
		if(this.isHideCols){
			this.hideCols(true);
		}
		//刷新表头
		this.setTitleWidth();
		this.initParam();
		this.initEvent();
	};
	
	ctrl.initParam = function() {
		//初始化下拉框列表信息
		this.dataList = {};
		var dataList = this.config.listData;
		if(dataList){
			for(var key in dataList) {
				var list = dataList[key];
				
				var option = '<option value=""> </option>';
				for(var i = 0; i < list.length; i++){
					var row = list[i];
					option += '<option value="' + row.code + '">' + row.name + '</option>';
				}
				var select = '<select class="selectpicker"  data-live-search="true" data-width="100px" data-actions-box="false" >' + option + '</select>';
				this.dataList[key] = select;
			}
		}
	}
	
	ctrl.initEvent = function() {
		var self = this;
		
		var jyytableSkip = $(this).find("#jyytable_skip");
		jyytableSkip.find("button").click(function(){
			var input = jyytableSkip.find("input");
			JyyTable.ctrl.onPageEvent(input, null, "skip", input.val());
		});
		
		var jyytableSize = $(this).find("#jyytable_size");
		jyytableSize.find("select").change(function(e){
			JyyTable.ctrl.onPageEvent(this, null, "size", $(this).val());
		});
		
		window.onresize = function () {
			JyyTable.ctrl.setTitleWidth();
		};
		
		var jyytable = $(this).find("#jyytable");
		var left = $(this).css("left");
		var offLeft = $(this).offset().left;
		jyytable.parent().scroll(function() {
			var jyytableTitle = $(self).find("#jyytable_title");
			jyytableTitle.parent().css("left", jyytable.offset().left - offLeft);
		});
	}

	ctrl.onPageEvent = function(e, originalEvent, type, page) {
		var currentPage = null;
		var pageSize = null;
		var jyytableSize = $("#jyytable_size");
		if(type == "page" || type == "first" || type == "prev" || type == "next" || type == "last"){
			currentPage = page;
			pageSize = jyytableSize.find("option:selected").val();
		}else if(type == "skip"){
			var isNum = JyyCommon.isPositiveInteger(page);
			if(!isNum) return;
			currentPage = page;
			pageSize = jyytableSize.find("option:selected").val();
			$(this).find("#jyytable_page").bootstrapPaginator("show", page);
			e.val("");
		}else if(type == "size"){
			currentPage = 1;
			pageSize = page;
		}
		var value = {currentPage:currentPage, pageSize:pageSize};
		JyyTable.ctrl.callback("page", value);
	}

	ctrl.setTitleWidth = function() {
		var index = 0;
		var jyytable = $(this).find("#jyytable");
		var jyytableTitle = $(this).find("#jyytable_title");
		var tdArea = jyytableTitle.find("thead td");
		jyytable.find("thead td").each(function() {
			$(tdArea[index]).width($(this).width());
			index++;
		})
		jyytableTitle.width(jyytable.width() + 2);
	}
	
	ctrl.showCheckBox = function() {
		var $thr = $(JyyTable.ctrl).find('table thead tr');
        var $checkAllTh = $('<td style="text-align: center;" name="checkbox"><input type="checkbox" name="checkAll" /></td>');
        $thr.prepend($checkAllTh);
        
        var $checkAll = $thr.find('input[type=checkbox]');
        $checkAll.click(function(event){
            var checkboxs = $(JyyTable.ctrl).find('table tbody tr').find('input[type=checkbox]');
            checkboxs.prop('checked', $(this).prop('checked'));
            if ($(this).prop('checked')) {
                checkboxs.parent().parent().addClass('warning');
            } else{
                checkboxs.parent().parent().removeClass('warning');
            }
            event.stopPropagation();
        });
        $checkAllTh.click(function(){
            $(this).find('input[type=checkbox]').click();
        });
        
        var $tbr = $(JyyTable.ctrl).find('table tbody tr');
        var $checkItemTd = $('<td style="text-align: center;" name="checkbox"><input type="checkbox" name="checkItem" /></td>');
        $tbr.prepend($checkItemTd);
        
    	$tbr.find('input[type=checkbox]').click(JyyTable.ctrl.checkboxClick);
        /*$tbr.click(function(){  
            $(this).find('input[type=checkbox]').click();
        });*/
	}
	
	ctrl.checkboxClick = function(event) {
		var $thr = $(this).find('table thead tr');
		var $tbr = $(this).find('table tbody tr');
		var $checkAll = $thr.find('input[type=checkbox]');
		$(this).parent().parent().toggleClass('warning');
        $checkAll.prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false);
        event.stopPropagation();
	}
	
	ctrl.showFun = function() {
		$(this).find('thead tr').append('<td style="width: 65px;" name="buttons">操作</td>');
		$(this).find('tbody tr').append(this.funButton);
	}
	
	ctrl.IterarCamposEdit = function($cols, tarea) {
	    var n = 0;
	    $cols.each(function() {
	        n++;
	        if ($(this).attr('name')=='buttons') return;
	        if ($(this).attr('name')=='checkbox') return;
	        
	        var index = JyyTable.ctrl.config.isShowCheckBox ? n-1 : n;
	        if($(this).parent().attr("exeType") != "insert"){
	        	if (!EsEditable(index)) return;
	        }
	        tarea($(this), index);
	    });
    
	    function EsEditable(idx) {
	    	var index = JyyTable.ctrl.config.editColsIndex;
	        if (index.length == 0) {
	            return true;
	        } else {
	        	var isTrue = false;
	            for (var i = 0; i < index.length; i++) {
	            	if (idx == index[i]) {
	            		isTrue = true;
	            		continue;
	            	}
	            }
	            return isTrue;
	        }
	    }
	}
	
	ctrl.FijModoNormal = function(but) {
	    $(but).parent().find('#jyytable_acep').hide();
	    $(but).parent().find('#jyytable_canc').hide();
	    $(but).parent().find('#jyytable_edit').show();
	    $(but).parent().find('#jyytable_elim').show();
	    var $row = $(but).parents('tr');
	    $row.attr('id', '');
	}
	
	ctrl.FijModoEdit = function(but) {
	    $(but).parent().find('#jyytable_acep').show();
	    $(but).parent().find('#jyytable_canc').show();
	    $(but).parent().find('#jyytable_edit').hide();
	    $(but).parent().find('#jyytable_elim').hide();
	    var $row = $(but).parents('tr');
	    $row.attr('showType', 'editing');
	}
	
	ctrl.ModoEdicion = function($row) {
	    if ($row.attr('showType')=='editing') {
	        return true;
	    } else {
	        return false;
	    }
	}
	
	ctrl.addRow = function() {
		var isAdd = this.checkAdd();
		if(isAdd){
			var input = null;
			var $jyytable = $(this).find("#jyytable");
		    var $tbodytr = $jyytable.find('tbody tr');
		    if ($tbodytr.length == 0) {//无数据行
		    	var htmltd = '';
		    	var $theadtr = $jyytable.find('thead tr');
		        var $td = $theadtr.find('td');
		        $td.each(function() {
		            if ($(this).attr('name') == 'buttons') {
		            	htmltd = htmltd + JyyTable.ctrl.funButton;
		            } else if($(this).attr('name') == 'checkbox') {
		            	htmltd = htmltd + '<td style="text-align: center;" name="checkbox"><input type="checkbox" name="checkItem"/></td>';
		            } else {
		            	var isVisible = $(this).is(':visible');
		            	if(isVisible){
		            		htmltd = htmltd + '<td></td>';
		            	}else{
		            		htmltd = htmltd + '<td style="display:none"></td>';
		            	}
		            }
		        });
		        $jyytable.find('tbody').append('<tr exeType="insert">' + htmltd + '</tr>');
		        
		        input = $jyytable.find('tbody input');
		        input.click(JyyTable.ctrl.checkboxClick);
		        
		    } else {//存在数据行
		        var $lasttr = $jyytable.find('tr:last');
		        $lasttr.clone().prependTo($lasttr.parent());
		        $lasttr = $jyytable.find('tr:eq(1)');
		        $lasttr.attr("exeType", "insert");
		        $lasttr.removeAttr("id");
		        
		        var $lasttd = $lasttr.find('td');
		        $lasttd.each(function() {
		            if ($(this).attr('name') == 'buttons') {
		            	JyyTable.ctrl.FijModoNormal($(this).find('#jyytable_edit'));
		            	
		            } else if($(this).attr('name') == 'checkbox') {
		            	input = $(this).find('input');
		            	input.click(JyyTable.ctrl.checkboxClick);
		            	
		            } else {
		            	$(this).removeAttr("code");
		                $(this).html('');
		            }
		        });
		    }
			//将其他选中状态取消，默认新添加行选中。
			var checkedTr = $tbodytr.find('input[type=checkbox]:checked').each(function(){
				$(this).click();
			});
			input[0].click();
			//改变成修改状态
			this.updateRows(input[0]);
			//刷新表头行
		    var tb = $jyytable.parent();
			if(tb[0].scrollWidth > tb[0].clientWidth){
				this.setTitleWidth();
			}
		}else{
			JyyMsgBox.alert({ title:"提示", message:"请先保存后再添加！",type:"warning"}).on(function (e) {});
			return;
		}
	}
	
	ctrl.setSelectpicker = function() {
		$(this).find('.selectpicker').selectpicker({
            noneSelectedText: '请选择',
            noneResultsText: '没有找到匹配的组织 {0}'
        });
        //$('.selectpicker').change(function(){
        //	JyyTable.ctrl.setTitleWidth();
        //});
        this.setTitleWidth();
	}
		
	ctrl.checkAdd = function() {
		var num = 0;
		$(this).find('table tbody tr').each(function () {
			if($(this).attr("exeType") == "insert"){
				num++;
			}
		})
		if(num > 0){
			return false;
		}
		return true;
	}
	
	ctrl.updateRows = function(obj) {
		var checked = $(this).find('table tbody tr input[type=checkbox]:checked');
		if(checked.length > 50){
			JyyMsgBox.alert({ title:"提示", message:"最多同时修改50条！",type:"warning"}).on(function (e) {});
		}else{
			checked.each(function () {
				if(obj){
					if(obj == this){
						var tr = $(this).parents('tr')[0];
				        var tableEdit = $(tr).find('#jyytable_edit');
				        JyyTable.funClick((tableEdit.length == 0 ? tr : tableEdit), "edit");
					}
				}else{
					var tr = $(this).parents('tr')[0];
					var exeType = $(tr).attr("exeType");
					if(exeType != "insert") $(tr).attr("exeType","update");
			        var tableEdit = $(tr).find('#jyytable_edit');
			        JyyTable.funClick((tableEdit.length == 0 ? tr : tableEdit), "edit");
				}
	        });
	        this.setSelectpicker();	
		}
	}
	
	ctrl.selectRows = function() {
		var trArea = [];
		var checkedTr = $(this).find('table tbody tr').find('input[type=checkbox]:checked');
		checkedTr.each(function () {
	        var tr = $(this).parents('tr')[0];
	        trArea[trArea.length] = tr;
        });
        var value = this.getValue(trArea, true);
		var isTrue = JyyTable.ctrl.callback("insertBefore", value);
		if(isTrue){
			checkedTr.each(function () {
		        var tr = $(this).parents('tr')[0];
		        $(tr).removeAttr("exeType");
		        var tableEdit = $(tr).find('#jyytable_edit');
		        JyyTable.funClick((tableEdit.length == 0 ? tr : tableEdit), "acep", true);
	        });
	        JyyTable.ctrl.callback("data", value);
		}
	}
	
	ctrl.cancelRow = function() {
		$(this).find('table tbody tr').find('input[type=checkbox]:checked').each(function () {
	        var tr = $(this).parents('tr')[0];
	        var tableEdit = $(tr).find('#jyytable_edit');
	        JyyTable.funClick((tableEdit.length == 0 ? tr : tableEdit), "cancel");
        });
	}
	
	ctrl.deleteRows = function() {
		var self = this;
		JyyMsgBox.confirm({ title:"提示", message:"确定删除数据？",type:"warning"}).on(function (e) {
			if (e) {
				var data = [];
		  		var rows = $(self).find('table tbody tr').find('input[type=checkbox]:checked').parents('tr').each(function () {
					if($(this).attr("exeType") != "insert"){
						$(this).attr("exeType", "delete")
						data[data.length] = this;
					}
					$(this).remove();
				});
				var value = JyyTable.ctrl.getValue(data, false);
				if(value.length > 1) {
					JyyTable.ctrl.callback("data", value);
				}else {
					JyyTable.ctrl.setTitleWidth();
				}
			}
		});
	}
	
	ctrl.saveRow = function() {
		var trArea = [];
		var checkedTr = $(this).find('table tbody tr').find('input[type=checkbox]:checked');
		checkedTr.each(function () {
	        var tr = $(this).parents('tr')[0];
	        trArea[trArea.length] = tr;
        });
        var value = this.getValue(trArea, true);
		var isTrue = JyyTable.ctrl.callback("insertBefore", value);
		if(isTrue){
			checkedTr.each(function () {
		        var tr = $(this).parents('tr')[0];
		        $(tr).removeAttr("exeType");
		        var tableEdit = $(tr).find('#jyytable_edit');
		        JyyTable.funClick((tableEdit.length == 0 ? tr : tableEdit), "acep", true);
	        });
	        JyyTable.ctrl.callback("data", value);
		}
	}
	
	ctrl.hideCols = function(isHide) {
		this.isHideCols = isHide;
		if(ctrl.config.hideCols.length > 0){
			var listIndex = this.config.hideCols;
			for(var i = 0; i < listIndex.length; i++){
				var index = listIndex[i];
				$(this).find('tr').each(function(){
					if(isHide){
						$(this).find("td:eq(" + index + ")").css("display","none");
					}else{
						$(this).find("td:eq(" + index + ")").css("display","table-cell");
					}
				})
			}
			//刷新表头
			this.setTitleWidth();
		}
	}
	
	ctrl.getValue = function(obj, isSave, isReal) {
		var value = [[]];
		
		var title = this.config.data[0];
		for(var i = 0; i < title.length; i++){
			value[0][i] = title[i].code;
		}
		if(!isReal)value[0].unshift("type");
		
		for(var i = 0; i < obj.length; i++){
			var row = obj[i];
			
			var isEdit = $(row).attr("showType") == "editing" ? true : false;
			var exeType = $(row).attr("exeType");
			
			var rowValue = [];
			var type = exeType ? exeType : "update";
			if(!isReal) rowValue[0] = type;
			
			$(row).find('td').each(function(){
				
				var code = $(this).attr("code");
				var name = $(this).attr("name");
				
				var cellValue = null;
				if(name == undefined){
					if(isEdit){
						if(isSave || type == "delete"){
							var input = $(this).find('input');
							if(input.length == 0){
								cellValue = this.innerText;
							}else{
								cellValue = $(this).find('input').val();
							}
						}else{
							cellValue = $(this).find('div').text();
						}
					}else{
						cellValue = this.innerText;
					}
					if(code){
						if(isReal){
							cellValue = code;
						}else{
							cellValue = {code:code, name:cellValue.trim()};
						}
					}
				}else if(name == "droplist"){
					if(isEdit){
						if(isSave){
							code = $(this).find('option:selected').val();
							cellValue = $(this).find('option:selected').html();
						}else{
							cellValue = $(this).find('div:eq(0)').text();
						}
					}else{
						cellValue = this.innerText;
					}
					if(code){
						if(isReal){
							cellValue = code;
						}else{
							cellValue = {code:code, name:cellValue.trim()};
						}
					}
				}else{
					return;
				}
				rowValue[rowValue.length] = cellValue;
			})
			value[value.length] = rowValue;
		}
		return value;
	}
	
	ctrl.setConfig = function(config, newConfig, scaleConfig) {
		for(key in config) {
			if(JyyCommon.isEmpty(newConfig[key])){
				config[key] = scaleConfig[key];
			} else if(JyyCommon.isObject(newConfig[key])){
				if(key == "listData"){
					config[key] = newConfig[key];
				}else{
					this.setConfig(config[key], newConfig[key], scaleConfig[key]);
				}
			}else{
				config[key] = newConfig[key];
			}
		}
	}
	
	ctrl.getCheckValue = function(){
		var tr = $(this).find('table tbody tr').find('input[type=checkbox]:checked').parents('tr');
		return JyyTable.ctrl.getValue(tr, true, true);
	}
	
	ctrl.refresh = function(config){
		this.setConfig(this.config, config, JyyTable.config);
		//this.isHideCols = true;
		this.init();
	}
	
	ctrl.refreshPage = function(num, currentPage){
		var totalSize = this.config.page.option.totalSize;
		totalSize = totalSize + num;
		var pageSize = this.config.page.option.pageSize;
		var totalPages = Math.ceil(totalSize/pageSize);
		
		this.config.page.option.currentPage = currentPage ? currentPage : 1;
		this.config.page.option.totalSize = totalSize;
		this.config.page.option.totalPages = totalPages;
		this.init();
	}
	
	ctrl.init();
	
	return ctrl;
}
JyyTable.funClick = function(but, type, isMore){
	
	var isTr = but.tagName ? (but.tagName.toLowerCase() == "tr" ? true : false) : false;
	if(type == "edit"){
		console.log("edit");
		
		var $row = isTr ? $(but) : $(but).parents('tr');
	    var $cols = $row.find('td');
	    if (this.ctrl.ModoEdicion($row)) return;
	    this.ctrl.IterarCamposEdit($cols, function($td, index) {
	    	var children = $td.children()[0];
		    if(children && children.type == 'checkbox'){
		    	return;
		    }
		    
		    var cont = $td.html();
		    var div = '<div style="display: none;">' + cont + '</div>';
			if(JyyTable.ctrl.dataList[index]){
				$td.html(div + JyyTable.ctrl.dataList[index]);
				$td.find('option').each(function(){
					if ($(this).text() == cont) {
				         $(this).attr("selected", "selected");
				    }
				})
				$td.attr('name', 'droplist');
			}else{
	        	var input = '<input style="padding: 0px 2px;width:60px" class="form-control input-sm"  value="' + cont + '">';
	        	$td.html(div + input);
	        	$td.removeAttr("name");
			};
	    });
	    if(!isTr){
	    	this.ctrl.FijModoEdit(but);
		}else{
		    $row.attr('showType', 'editing');
		}
		this.ctrl.setTitleWidth();
	}else if(type == "elim"){
		console.log("elim");
		JyyMsgBox.confirm({ title:"提示", message:"确定删除数据？",type:"warning"}).on(function (e) {
		  if (e) {
		  	var $row = $(but).parents('tr');
			    JyyTable.ctrl.callback("data", $row);
			    $row.remove();
			    JyyTable.ctrl.setTitleWidth();
			}
		});
	}else if(type == "acep"){
		console.log("acep");
		var $row = isTr ? $(but) : $(but).parents('tr');
	    var $cols = $row.find('td');
	    if (!this.ctrl.ModoEdicion($row)) return;
	    
	    var value = this.ctrl.getValue($row, true);
	    var isTrue = isMore ? isMore : this.ctrl.callback("insertBefore", value);
	    if(isTrue){
		    this.ctrl.IterarCamposEdit($cols, function($td, index) {
				var children = $td.children()[0];
				if(children && children.type == 'checkbox'){
					return;
				}
				var value = "";
				if(JyyTable.ctrl.dataList[index]){
			  		value = $td.find("option:selected").text();
			  	}else{
					value = $td.find('input').val();
				}
				$td.html(value);
		    });
			if(!isTr){
				this.ctrl.FijModoNormal(but);
			}else{
				$row.attr('id', '');
			}
			this.ctrl.setTitleWidth();
			if($row.attr("exeType") == "insert"){
				if(!isMore) this.ctrl.callback("data", value);
				$row.removeAttr("exeType");
			}else{
				if(!isMore) this.ctrl.callback("data", value);
			}
		}
	}else if(type == "cancel"){
		console.log("cancel");
		var $row = isTr ? $(but) : $(but).parents('tr');
	    var $cols = $row.find('td');
	    if (!this.ctrl.ModoEdicion($row)) return;
	    $row.removeAttr("showType");
	    this.ctrl.IterarCamposEdit($cols, function($td) {
	        var cont = $td.find('div').html();
	        $td.html(cont);
	    });
	    if(!isTr){
			this.ctrl.FijModoNormal(but);
		}else{
			$row.attr('id', '');
		}
		this.ctrl.setTitleWidth();
	}
}
//ED*****************************************表格编辑组件***************************************